package com.calculation;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class EquationTest {
    @Test
    public void testGenerateRandom() {
        Equation eq = new Equation();
        eq.generateRandom();
        // 验证操作数在0-100范围内
        assertTrue(eq.getOperand1() >= 0 && eq.getOperand1() <= 100);
        assertTrue(eq.getOperand2() >= 0 && eq.getOperand2() <= 100);
        // 验证运算符是+或-
        assertTrue(eq.getOperator() == '+' || eq.getOperator() == '-');
    }

    @Test
    public void testToString() {
        // 方法1：使用setter
        Equation eq1 = new Equation();
        eq1.setOperand1(15);
        eq1.setOperand2(7);
        eq1.setOperator('+');
        assertEquals("15 + 7 = ", eq1.toString());
        // 方法2：使用构造函数
        Equation eq2 = new Equation(8, 3, '-');
        assertEquals("8 - 3 = ", eq2.toString());
    }

    @Test
    public void testAdditionRange() {
        Equation eq = new Equation();
        for (int i = 0; i < 100; i++) {
            eq.generateRandom();
            if (eq.getOperator() == '+') {
                assertTrue(eq.getOperand1() + eq.getOperand2() <= 100);
            }
        }
    }

    @Test
    public void testSubtractionRange() {
        Equation eq = new Equation();
        for (int i = 0; i < 100; i++) {
            eq.generateRandom();
            if (eq.getOperator() == '-') {
                assertTrue(eq.getOperand1() - eq.getOperand2() >= 0);
            }
        }
    }
}